const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

var path = require('path');
module.exports = {
  mode: "production",
  entry: {
    main: './main.js'
  },
  output: {
    path: path.join(__dirname, "build"),
    publicPath: './',
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 100,
            },
          },
           {
            loader: 'image-webpack-loader',
            options: {
              pngquant: {
                quality: [0.25, 0.60],
                speed: 4
              },
            },
          },
        ],
      },
      {
        test: /\.(m?js)|(jsx)$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', "@babel/preset-react"],
            plugins: [
              '@babel/plugin-transform-runtime'
            ]
          }
        }
      },
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
        // use: [
        //   //执行顺序，从有向左
        //   { loader: "style-loader" },
        //   { loader: "css-loader" }
        // ]
      }
    ]
  },
  optimization: {
    minimizer: [new OptimizeCSSAssetsPlugin({})],
  },
  // 配置插件
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html', // 目标文件名
      template: './index.html' // 源文件路径
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      chunkFilename: '[id].css',
    }),
  ]
};